package huaweiod.first;

import java.util.*;

/**
 * 华为OD机试真题 Java 实现【过滤组合字符串】【2022.11 Q4新题】
 */
public class A_2023_13_FilterCombinedString {

    public static void combinedString(String num_str, HashMap<Character, String> map, int index,
                                      List<Character> list, List<String> result) {
        if (num_str.length() == index) {
            StringBuilder tempStr = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                tempStr.append(list.get(i));
            }
            result.add(tempStr.toString());
            return;
        }
        for (char c : map.get(num_str.charAt(index)).toCharArray()) {
            list.add(c);
            combinedString(num_str, map, index + 1, list, result);
            list.remove(list.size() - 1);
        }
    }

    public static boolean filterString(String str1, String str2) {
        Set<Character> set1 = new HashSet<>();
        for (char c : str1.toCharArray()) {
            set1.add(c);
        }

        Set<Character> set2 = new HashSet<>();
        for (char c : str2.toCharArray()) {
            set2.add(c);
        }

        for (Character c : set2) {
            if (!set1.contains(c)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String num_str = in.nextLine();
        String block_str = in.nextLine();
        //预设值
        HashMap<Character, String> num_char_map = new HashMap<Character, String>();

        num_char_map.put('0', "abc");
        num_char_map.put('1', "def");
        num_char_map.put('2', "ghi");
        num_char_map.put('3', "jkl");
        num_char_map.put('4', "mno");
        num_char_map.put('5', "pqr");
        num_char_map.put('6', "st");
        num_char_map.put('7', "uv");
        num_char_map.put('8', "wx");
        num_char_map.put('9', "yz");

        // 组合字符串
        List<String> result = new ArrayList<>();
        List<Character> list = new ArrayList<>();
        combinedString(num_str, num_char_map, 0, list, result);
        String ans = "";
        // 过滤包括的字符串
        for (String s : result) {
            if (!filterString(s, block_str)) {
                ans = ans + s + " ";
            }
        }
        System.out.println(ans);
    }
}
